package com.example.satokendemospringboot;

import cn.dev33.satoken.stp.StpUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String reqUrl =  request.getRequestURI();
        String appToken = request.getHeader("X-Access-Token");
        String appLanguage = request.getHeader("lang");

        System.err.println("reqUrl-->"+reqUrl);
        System.err.println("appToken-->"+appToken);
        System.err.println("appLanguage-->"+appLanguage);


        if (StringUtils.isEmpty(appToken)){
            throw new BaseException(5001,"reqHeader appToken is null");
        }else if (StpUtil.getLoginIdByToken(appToken) == null){
            throw new BaseException(5002,"appToken is expired");
        }else {
            return true;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
